//
//  ViewController.m
//  14-CollectionViewBanTang
//
//  Created by 曹曹 on 16/8/2.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "ViewController.h"
#import "MyCollectionViewCell.h"
#import "LabelCollectionReusableView.h"
#import "CollectionViewAndLabelReusableView.h"

#define kCollectionViewReuse @"collectionCell"
#define kCollectionViewHeader @"collectionHead"
#define kCollectionViewHeaderNoCell @"collectionHeadNoCell"
#define kArc4RandomColor [UIColor colorWithRed:arc4random() % 256 / 255.0f green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:1]
#define kItemWidth (CGRectGetWidth(self.view.bounds) - 10) / 3

@interface ViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self topCollectionView];
    
}

- (void)topCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // item大小
    layout.itemSize = CGSizeMake(kItemWidth, kItemWidth);
    // 列间距
    layout.minimumInteritemSpacing = 0;
    // 行间距
    layout.minimumLineSpacing = 5;
    // 区头大小
    layout.headerReferenceSize = CGSizeMake(0, 50);
    // init
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    // cell
    [collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewReuse];
    // header
    [collectionView registerClass:[LabelCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeader];
    [self.view addSubview:collectionView];
    // 1.通过上边距实现视图添加(UICollectionView本质是UIScrollView)
    // collectionView.contentInset = UIEdgeInsetsMake(400, 0, 0, 0);
    // UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -395, CGRectGetWidth(self.view.bounds), 390)];
    // view.backgroundColor = [UIColor greenColor];
    // [collectionView addSubview:view];
    // 2.自定义一个头视图
    [collectionView registerClass:[CollectionViewAndLabelReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeaderNoCell];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

// 修改某个item的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeMake(0, 400);
    } else {
        return CGSizeMake(0, 50);
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        CollectionViewAndLabelReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeaderNoCell forIndexPath:indexPath];
        return view;
    } else {
        LabelCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeader forIndexPath:indexPath];
        return reusableView;
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else {
        return 100;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewReuse forIndexPath:indexPath];
    cell.contentView.backgroundColor = kArc4RandomColor;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
